package com.buddies.lottery.ui.withdraw;

import com.buddies.lottery.base.BasePresenter;
import com.buddies.lottery.common.ConfigManager;
import com.buddies.lottery.data.BaseObserver;
import com.buddies.lottery.data.DataManager;
import com.buddies.lottery.data.bean.Wallet;
import com.buddies.lottery.data.resp.WithdrawResponse;

/**
 * Created by xuliang on 2018/2/28.
 */

public class WithdrawPresenter extends BasePresenter<WithdrawContract.View> implements WithdrawContract.Presenter {

    public WithdrawPresenter(WithdrawContract.View view) {
        super(view);
    }

    @Override
    public void postQrcode(String qrcodeUrl) {
        addSubscription(DataManager.getInstance().getUserRepo().postQrcode(qrcodeUrl), new BaseObserver<Object>() {
            @Override
            public void onNext(Object response) {
                mView.onQrcodePostSuccess();
            }

            @Override
            public void onError(Throwable e) {
               mView.onError(e);
            }
        });
    }

    @Override
    public void postWithdraw(int money, String pwd) {
        addSubscription(DataManager.getInstance().getUserRepo().postWithdraw(money, pwd), new BaseObserver<Wallet>() {
            @Override
            public void onNext(Wallet response) {
                ConfigManager.getInstance().setWallet(response);
                mView.onWithdrawPostSuccess();
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }

    @Override
    public void loadWithdrawInfo() {
        addSubscription(DataManager.getInstance().getUserRepo().getWithdrawInfo(), new BaseObserver<WithdrawResponse>() {
            @Override
            public void onNext(WithdrawResponse response) {
                ConfigManager.getInstance().setWallet(response.wallet);
                mView.onWithdrawInfoLoaded(response);
            }

            @Override
            public void onError(Throwable e) {
                mView.onError(e);
            }
        });
    }
}
